<?php
class Registration_model extends CI_Model {
	

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
	
	function check()
    {
		$this->load->helper('url');
		//
        $query = null; // emptying in case 
		
		$promotion	= 'Promotion Name'; 	// hard-coded or set in config file
        $first_name = $_POST['first_name'];
		$last_name 	= $_POST['last_name'];
		$gender 	= $_POST['gender']; 	// unused
		$email 		= $_POST['email'];
		$facebook 	= $_POST['facebook']; 	// unused
		$phone	 	= $_POST['phone']; 
		$address	= $_POST['address']; 
		$city	 	= $_POST['city']; 
		$state	 	= $_POST['state']; 
		$zip_code	= $_POST['zip_code']; 
		//
		$optin = $this->input->post('optin');
		((int) $optin == 1) ? $optin = 1 : $optin = 0;
		//
		$date	 	= date('Y-m-d H:i:s');	// NOW() replacement
		

		// select
        $query = $this->db->get_where('registrations', array( // matching criteria array
            'email' => $email
        ));
		// count
        $count = $query->num_rows(); // counting result from query
		
		// if NO exist
        if ($count === 0) 
		{
            $data = array(
				'promotion' 	=> $promotion,
                'first_name' 	=> $first_name,
				'last_name' 	=> $last_name,
				'gender' 		=> $gender,	
				'email' 		=> $email,
				'facebook'		=> $facebook, 
				'phone'			=> $phone, 
				'address'		=> $address,
				'city'			=> $city,
				'state'			=> $state, 
				'zip_code'		=> $zip_code,
				'optin'			=> $optin,
				'date'			=> $date,
            );
            $this->db->insert('registrations', $data);
			//
			if($this->db->affected_rows() > 0)
			{
			  // echo 'row succesfully inserted';
			  //
			  $this->email_confirmation($data, $first_name, $last_name, $email);
			}
        }
		else
		{
			// send confirmation email regardless of user existing in DB or inserted
			//
			$this->email_confirmation($data, $first_name, $last_name, $email);
		}
		
		
    }
	
	
	function email_confirmation($data, $first_name, $last_name, $email)
    {
		$this->load->helper('url');
		//
		$config = Array(
			//'protocol' 	=> 'smtp', 	// won't work in GoDaddy. use "senmail" instead.
			'protocol' 	=> 'sendmail',
			'smtp_host' => 'ssl://smtp.googlemail.com',
			'smtp_port' => 465,
			'smtp_user' => 'mgscomm.digital@gmail.com',
			'smtp_pass' => 'cart3l1t072',
			'mailtype'  => 'html', 	// It works in GoDaddy.
			//'mailtype'  => 'text', 
			'charset'   => 'iso-8859-1'
			);
		$this->load->library('email', $config);
		$this->email->set_newline("\r\n"); // newlines always in double quotes
		//
		$this->email->from('mgscomm.digital@gmail.com', 'MGSCOMM Digital');
		$list = array($email); // comma sepparated list for multiple recipients
		$this->email->to($list);
		$this->email->reply_to('mgscomm.digital@gmail.com', 'Explendid Videos');
		$this->email->subject('This is an email test');
		//$this->email->message('<h1>It is working. Great!</h1>');
		$this->email->message($this->load->view('emails/confirmation', $data, TRUE)); 				// It works in GoDaddy.
    	//$this->email->set_alt_message($this->load->view('emails/confirmation-alt', '', TRUE));	// 
		$this->email->send();
		
		// test output:
		// $this->load->view('emails/confirmation', $data);  // first_name passes to view. Just use:  "echo ucfirst($first_name);" in view's HTML.  

	}

}